package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealMapper setmelMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId(); //在拦截器中解析出来的当前用户id，存放在线程中，通过BaseContext获取
        shoppingCart.setUserId(currentId); //设置当前用户id
        log.info("购物车·当前用户id：{}",currentId);
        //判断当前购物车中是否已经存在该商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果已经存在，就在原来数量基础上加一
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            log.info("购物车·查询结果：{}",list.get(0));
            cart.setNumber(cart.getNumber()+1);  //数量加一
            shoppingCartMapper.updateNumberById(cart); //更新数量
        }
        //如果不存在，就直接插入一条数据
        else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //判断当前是菜品还是套餐,
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //根据菜品id查询菜品，还有口味数据
                //查询菜品
                Dish dish1 = dishMapper.getById(dishId);
                //前端传入的口味是字符串也就是真实数据，所以不需要查询数据库dto中自带口味数据
                shoppingCart.setName(dish1.getName());
                shoppingCart.setImage(dish1.getImage());
                shoppingCart.setAmount(dish1.getPrice());
            }
            else{
                //根据套餐id查询套餐，dishId为空就表示当前是套擦
                Setmeal setmeal1 = setmelMapper.getById(shoppingCartDTO.getSetmealId());
                log.info("购物车·查询结果：{},{}",setmeal1,shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal1.getName());
                shoppingCart.setImage(setmeal1.getImage());
                shoppingCart.setAmount(setmeal1.getPrice());
            }

            //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }





    }

    @Override
    public List<ShoppingCart> list() {

        //查询购物车,jwt中解析出来的当前用户id
        Long currentId = BaseContext.getCurrentId();

        List<ShoppingCart> listCart= shoppingCartMapper.getById(currentId);

        return listCart;
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //判断当前商品是否存在，如果存在就减一，如果不存在就直接删除
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前商品是否存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list!= null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            //如果数量大于1，就减1
            if(cart.getNumber() > 1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }else {
                shoppingCartMapper.delete(shoppingCart);
            }
        }

    }

    @Override
    public void cleanShoppingCart() {
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }
}
